package com.ray.controller;

import java.text.ParseException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ray.exception.ServiceException;
import com.ray.service.EnergyService;
import com.ray.service.MaterService;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;
import com.ray.util.ReturnStringUtil;

@Controller
@RequestMapping("/energy")
public class EnergyController {
	@Autowired
	EnergyService energyService;
	@Autowired
	MaterService materService;
	
	//获取所有电表
	@RequestMapping("/getAllElectricEnergy")
	@ResponseBody
	public Map getAllElectricEnergy(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(energyService.getAllElectricEnergy(map));
	}
	//获取所有电表
		@RequestMapping("/getAllFlowEnergy")
		@ResponseBody
		public Map getAllFlowEnergy(HttpServletRequest request) throws ServiceException {
			Map map=RequestUtil.getRequestAllParam(request);
			return ResponseUtil.ResponseResult(energyService.getAllFlowEnergy(map));
		}
	
	/**
	 * 获取所有仪表数据
	 * @param start(分页参数)
	 * @param length(分页参数)
	 * @param request
	 * @return
	 * @throws ServiceException
	 * @throws ParseException 
	 * 父路径/energy
	 */
	@RequestMapping("/getAllEnergy")
	@ResponseBody
	public Map getAllEnergy(HttpServletRequest request) throws ServiceException, ParseException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(energyService.getAllEnergy(map));
	}
	
	
	/**
	 * 获取所有仪表   
	 * 不去分公司
	 * @param start(分页参数)
	 * @param length(分页参数)
	 * @param request
	 * @return
	 * @throws ServiceException
	 * @throws ParseException 
	 * 父路径/energy
	 */
	@RequestMapping("/getAllEnergyNotDifferenceCompany")
	@ResponseBody
	public Map getAllEnergyNotDifferenceCompany(HttpServletRequest request) throws ServiceException, ParseException {
		Map map=RequestUtil.getRequestAllParamNotLogin(request);
		map.put("type", 1);
		return ResponseUtil.ResponseResult(energyService.getAllEnergy(map));
	}
	
	/**
	 * 设置仪表状态为采集
	 * @param id(仪表id)
	 * @param request
	 * @return 设置成功
	 * @throws ServiceException
	 * @throws ParseException
	 * 父路径/energy
	 */
	@RequestMapping("/setEnergyAction")
	@ResponseBody
	public Map setEnergyAction(HttpServletRequest request) throws ServiceException, ParseException {
		Map map=RequestUtil.getRequestAllParam(request);
		energyService.setEnergyAction(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.SET_SUCCESS);
	}
	
	@RequestMapping("/getEnergyRecordsSum")
	@ResponseBody 
	public Map<String,Object> getEnergyRecordsSum(HttpServletRequest request) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(energyService.getEnergyRecordsSum(map));
	}
	
	@RequestMapping("/getLeisureElectricEnergy")
	@ResponseBody 
	public Map<String,Object> getLeisureElectricEnergy(HttpServletRequest request) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(energyService.getLeisureElectricEnergy(map));
	}
	
	@RequestMapping("/getLeisureFlowEnergy")
	@ResponseBody 
	public Map<String,Object> getLeisureFlowEnergy(HttpServletRequest request) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(energyService.getLeisureFlowEnergy(map));
	}
	
	
	/**
	 * 设置仪表所属代表车间
	 * workShopIdArray 车间id字符串数组  json格式
	 * energyId 仪表id
	 * imgUrl 图片地址
	 * @param request
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("/setEnergyWorkShop")
	@ResponseBody 
	public Map<String,Object> setEnergyWorkshop(HttpServletRequest request) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		energyService.setEnergyWorkshop(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.SET_SUCCESS);
	}
	
	/**
	 * 获取公司车间能耗信息
	 * @param request
	 * @return
	 * @throws ServiceException
	 * @throws ParseException 
	 */
	@RequestMapping("/getWorkEnergyConsumptionData")
	@ResponseBody 
	public Map<String,Object> getWorkEnergyConsumptionData(HttpServletRequest request) throws ServiceException, ParseException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(energyService.getWorkEnergyConsumptionData(map));
	}
	
	
	/**
	 * 获取仪表今天之前的使用情况
	 * 获取仪表年和月最大使用值（不含当天），和当月不含当天的使用量
	 * isUpdate  是否更新缓存   当有值（无论何值） 都更新缓存
	 * @param request
	 * @return
	 * @throws ServiceException
	 * @throws ParseException 
	 */
	@RequestMapping("/getEnergyTodayBeforeData")
	@ResponseBody 
	public Map<String,Object> getEnergyTodayBeforeData(HttpServletRequest request) throws ServiceException, ParseException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(energyService.getEnergyTodayBeforeDataFromRedis(map));
	}
	
	
	/**
	 * 获取仪表今天的使用情况
	 * @param request
	 * @return
	 * @throws ServiceException
	 * @throws ParseException 
	 */
	@RequestMapping("/getEnergyTodayData")
	@ResponseBody 
	public Map<String,Object> getEnergyTodayData(HttpServletRequest request) throws ServiceException, ParseException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(energyService.getEnergyTodayData(map));
	}
	
	
	/**
	 * 添加仪表
	 * jsonObject
	 * 仪表编号  no
	 * 仪表名称  name
	 * 仪表位置 address
	 * 变比（整数） variableRatio 变比（整数）
	 * 图片 imgUrl 
	 * 仪表所属车间 workShopIdList workShopId isDefaultShow是否显示1 显示 0 不显示
	 * energyType 仪表类型   1 电表  2 水表  3气表 电表时变比必填
	 */
	@RequestMapping("/addEnergy")
	@ResponseBody 
	public Map<String,Object> addEnergy(HttpServletRequest request) throws ServiceException, ParseException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(energyService.addEnergy(map));
	}
	
	/**
	 * 其他公司单独部署申请注册仪表
	 * @param request
	 * @param jsonStr
	 * @return
	 * @throws ServiceException
	 * @throws ParseException
	 */
	@RequestMapping("/addEnergyByOtherRequest")
	@ResponseBody 
	public String  addEnergyByOtherRequest(HttpServletRequest request,String jsonStr) throws ServiceException, ParseException{
	   String returnStr= energyService.addEnergyByOtherRequest(jsonStr);
		Assert.isTrue(false,"测试");
		return "SUCCESS";
	}
	
	
	/**
	 * 其他公司单独部署申请读取仪表数据
	 */
	
	
	
	
	/**
	 * 仪表回调接口
	 * @param request
	 * @return
	 * @throws ServiceException
	 * @throws ParseException
	 */
	@RequestMapping("/materResponseUrl")
	@ResponseBody 
	public String  materResponseUrl(HttpServletRequest request,String response_content) {
		return materService.setElementValues(response_content);
	}
	
	
	/**
	 * 仪表回调接口
	 * @param request
	 * startTime 开始时间
	 * endTime 结束时间
	 * type 类型  
	 * @return
	 * @throws ServiceException
	 * @throws ParseException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/materConsumption")
	@ResponseBody 
	public Map<String,Object> materConsumption(HttpServletRequest request) throws ServiceException, ParseException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(energyService.materConsumption(map));
	}
	
	
	/**
	 * 设置仪表是否可用
	 * jsonArray 
	 * energyNo  仪表编号
	 * isAgree 是否同意使用  1 同意 2 不同意
	 * @param request
	 * @return
	 * @throws ServiceException
	 * @throws ParseException
	 */
	@RequestMapping("/setEnergyIsCanUsed")
	@ResponseBody 
	public Map<String,Object> setEnergyIsCanUsed(HttpServletRequest request) throws ServiceException, ParseException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(energyService.setEnergyIsCanUsed(map));
	}
	
	
//	/**
//	 * 仪表回调接口
//	 * @param request
//	 * @return
//	 * @throws ServiceException
//	 * @throws ParseException
//	 */
//	public Map<String,Object> materCallBakUrl(HttpServletRequest request) throws ServiceException, ParseException{
//		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
//		return ResponseUtil.ResponseResult(energyService.setEnergyIsCanUsed(map));
//	}
	
	/**
	 * 校验仪表是否使用
	 * energyId 仪表编号
	 */
	@RequestMapping("/checkEnergyIsUsed")
	@ResponseBody 
	public Map<String,Object> checkEnergyIsUsed(HttpServletRequest request) {
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(energyService.checkEnergyIsUsed(map));
	}
}
